<?php
class Core_System 
	extends Core_Abstract 
{
	const ALL_SYSTEMS	= 'ALL';
	
	const B_OCCUPATION		= 1;
	const B_WEALTH			= 2;
	const B_REPUTATION		= 4;
	const B_ACTIONPOINTS	= 8;
	const B_POWERPOINTS		= 16;
	
	protected $id;
	
	protected $common;
	
	protected $version;
	
	protected $settings	= 0;
	
	protected $descp;
	
	protected $books	= array();
	
	public function setDescp($descp)
	{
		$this->descp	= $descp;
	}
	
	public function getDescp()
	{
		return $this->descp;
	}
	
	public function setId($id)
	{
		$this->id	= (int) $id;
	}
	
	public function getId()
	{
		return $this->id;
	}
	
	public function setCommon($common)
	{
		$this->common	= (string) $common;
	}
	
	public function getCommon()
	{
		return $this->common;
	}
	
	public function setVersion($version)
	{
		$this->version	= (float) $version;
	}
	
	public function getVersion()
	{
		return $this->version;
	}
	
	public function addBook(Core_Book $book)
	{
		$this->books[]	= $book;
	}
	
	public function getBooks()
	{
		return $this->books;
	}
	
	public function setSettings($settings)
	{
		$this->settings	= (int) $settings;
	}	
	
	public function checkSetting($setting)
	{
		return ($this->settings & $setting) > 0;
	}
	
	public function getPowerPoints()
	{
		return $this->checkSetting(self::B_POWERPOINTS);
	}
	
	public function setPowerPoints($pp)
	{
		//turn off
		if ($pp && !$this->getPowerPoints())
		{
			$this->settings += self::B_POWERPOINTS;
		}
		
		//turn off
		if (!$pp && $this->getPowerPoints())
		{
			$this->settings -= self::B_POWERPOINTS;
		}
	}
	
	public function getActionPoints()
	{
		return $this->checkSetting(self::B_ACTIONPOINTS);
	}
	
	public function setActionPoints($pp)
	{
		//turn off
		if ($pp && !$this->getActionPoints())
		{
			$this->settings += self::B_ACTIONPOINTS;
		}
		
		//turn off
		if (!$pp && $this->getActionPoints())
		{
			$this->settings -= self::B_ACTIONPOINTS;
		}
	}
	public function getReputation()
	{
		return $this->checkSetting(self::B_REPUTATION);
	}
	
	public function setReputation($rep)
	{
		//turn off
		if ($rep && !$this->getReputation())
		{
			$this->settings += self::B_REPUTATION;
		}
		
		//turn off
		if (!$rep && $this->getReputation())
		{
			$this->settings -= self::B_REPUTATION;
		}
	}
	
	public function getOccupation()
	{
		return $this->checkSetting(self::B_OCCUPATION);
	}
	
	public function setOccupation($occ)
	{
		//turn off
		if ($occ && !$this->getOccupation())
		{
			$this->settings += self::B_OCCUPATION;
		}
		
		//turn off
		if (!$occ && $this->getOccupation())
		{
			$this->settings -= self::B_OCCUPATION;
		}
	}
	
	public function getWealth()
	{
		return $this->checkSetting(self::B_WEALTH);
	}
	
	public function setWealth($wealth)
	{
		//turn off
		if ($wealth && !$this->getWealth())
		{
			$this->settings += self::B_WEALTH;
		}
		
		//turn off
		if (!$wealth && $this->getWealth())
		{
			$this->settings -= self::B_WEALTH;
		}
	}
	
	
	public function toXml(SimpleXMLElement $xml = null)
	{
		if (null == $xml)
		{
			$xml	= simplexml_load_string('<system></system>');
		}
		
		$xml->addChild('id', $this->getId());
    	$xml->addChild('name', htmlentities($this->getCommon(), ENT_QUOTES));
    	$xml->addChild('version', $this->getVersion());
    	$xml->addChild('description', htmlentities($this->getDescp(), ENT_QUOTES));
    	
    	$paramXml	= $xml->addChild('params');
    	$paramXml->addChild('wealth')->addAttribute('value', (int) $this->getWealth());
    	$paramXml->addChild('occupation')->addAttribute('value', (int) $this->getOccupation());
    	$paramXml->addChild('action')->addAttribute('value', (int) $this->getActionPoints());
    	$paramXml->addChild('reputation')->addAttribute('value', (int) $this->getReputation());
    	$paramXml->addChild('powerpoints')->addAttribute('value', (int) $this->getPowerPoints());

    	/*$bookXml	= $xml->addChild('books');
    	foreach ($this->getBooks() as $book)
    	{
    		$book->toXml($bookXml->addChild('book'));    		
    	}
    	*/
    	return $xml;		
	}
}